import { getAuthMenuApi } from "@/api/auth";
import router from "@/router"; // router.js  => rotuer/index.js

export default {
  namespaced: true,

  state: {
    tableData: [],
  },
  getters: {},
  mutations: {
    setTableData(state, payload) {
      state.tableData = payload;
    },
    deleteTableData(state) {
      state.tableData = [];
    },
  },
  actions: {
    async FETCH_MENU(context) {
      const res = await getAuthMenuApi();
      if (res.meta.state === 200) {
        // 1 保存到模型中
        context.commit("setTableData", res.data);
        // 2 动态路由
        res.data.forEach((oneMenu) => {
          if (oneMenu.children && oneMenu.children.length > 0) {
            oneMenu.children.forEach((twoMenu) => {
              // console.log(twoMenu);
              router.addRoute("admin", {
                path: twoMenu.url,
                component: () => import("@/views/" + twoMenu.component),
                meta: {
                  name1: twoMenu.auth_pname,
                  name2: twoMenu.auth_name,
                  keep_alive: twoMenu.keep_alive,
                },
              });
            });
            // console.log(router);
          }
        });

        // ....
      }
    },
  },
};
